﻿using FriendStar.Code;
using FriendStar.Domain.Entity.VBaseData;
using FriendStar.Domain.IRepository.VBaseData;
using FriendStar.Repository.VBaseData;
using System.Collections.Generic;
using System.Linq;
using SqlSugarRepository;
using System;

namespace FriendStar.Application.VBaseData
{
    public class V_CPK_11_DJ_StoRegisterApp
    {
        private IV_CPK_11_DJ_StoRegisterRepository service = new V_CPK_11_DJ_StoRegisterRepository();
        public List<V_CPK_11_DJ_StoRegisterEntity> GetList(string keyword = "",string type="")
        {
            var expression = ExtLinq.True<V_CPK_11_DJ_StoRegisterEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.UseUnit.Contains(keyword));
                expression = expression.Or(t => t.FProductionNO.Contains(keyword));
            }
            if(!string.IsNullOrEmpty(type))
            {
                expression = expression.And(p => p.FDocuTypeNO == type);
            }
            return service.IQueryable(expression).OrderBy(t => t.UseUnit).ToList();
        } 
        public List<V_CPK_11_DJ_StoRegisterEntity> GetList(Pagination pagination, string FHappenCompanyNO, string FProductionNO, string BeginFCurDate, string EndFCurDate, string FVoucherNo,string type = "")
        {
            var expression = ExtLinq.True<V_CPK_11_DJ_StoRegisterEntity>();
            if (!string.IsNullOrEmpty(FHappenCompanyNO))//FHappenCompanyNO
            {
                expression = expression.And(t => t.FHappenCompanyNO==FHappenCompanyNO);
            }
            if (!string.IsNullOrEmpty(FProductionNO))//FProductionNO
            {
                expression = expression.And(t => t.FProductionNO.Contains(FProductionNO));
            }
            if (!string.IsNullOrEmpty(BeginFCurDate))
            {
                //expression = expression.And(t => t.FCurDate.);
            }
            if (!string.IsNullOrEmpty(FVoucherNo))
            {
                expression = expression.And(t => t.FVoucherNo.Contains(FVoucherNo));
            }
            if (!string.IsNullOrEmpty(type))
            {
                expression = expression.And(p => p.FDocuTypeNO == type);
            }
            return service.FindList(expression, pagination);
        }

        public List<V_CPK_11_DJ_StoRegisterEntity> GetListRegIn(Pagination pagination, string FHappenCompanyNO, string FProductionNO, string BeginFCurDate, string EndFCurDate, string FVoucherNo, string type = "")
        {
            var expression = ExtLinq.True<V_CPK_11_DJ_StoRegisterEntity>();
            string extWhere = "";
            Dictionary<string, string> whereobj = new Dictionary<string, string>();
            if (!string.IsNullOrEmpty(FHappenCompanyNO))
            {
                expression = expression.And(t => t.FHappenCompanyNO == FHappenCompanyNO);
            }
            if (!string.IsNullOrEmpty(FProductionNO))
            {
                expression = expression.And(t => t.FProductionNO.Contains(FProductionNO));
            }
            if (!string.IsNullOrEmpty(BeginFCurDate))
            {
                if(!string.IsNullOrEmpty(extWhere))
                {
                    extWhere += " and ";
                }
                extWhere = " FCurDate>=@BeginFCurDate ";
                whereobj.Add("BeginFCurDate", BeginFCurDate);
            }
            if(!string.IsNullOrEmpty(EndFCurDate))
            {
                if(!string.IsNullOrEmpty(extWhere))
                {
                    extWhere += " and ";
                }
                extWhere += " FCurDate<=@EndFCurDate ";
                whereobj.Add("EndFCurDate", EndFCurDate);
            }
            if (!string.IsNullOrEmpty(FVoucherNo))
            {
                expression = expression.And(t => t.FVoucherNo.Contains(FVoucherNo));
            }
            if (!string.IsNullOrEmpty(type))
            {
                expression = expression.And(p => p.FDocuTypeNO == type);
            }
            else
            {
                expression = expression.And(p => p.FDocuTypeNO == "CR" || p.FDocuTypeNO=="CW");
            }
            return service.FindList(expression, pagination,extWhere,whereobj);
        }


        public List<V_CPK_11_DJ_StoRegisterEntity> GetListForPick(Pagination pagination, string FHappenCompanyNO, string FProductionNO, string BeginFCurDate, string EndFCurDate, string FVoucherNo, string type = "")
        {
            var expression = ExtLinq.True<V_CPK_11_DJ_StoRegisterEntity>();
            string extWhere = "";
            Dictionary<string, string> whereobj = new Dictionary<string, string>();
            if (!string.IsNullOrEmpty(FHappenCompanyNO))//FHappenCompanyNO
            {
                expression = expression.And(t => t.FHappenCompanyNO == FHappenCompanyNO);
            }
            if (!string.IsNullOrEmpty(FProductionNO))//FProductionNO
            {
                expression = expression.And(t => t.FProductionNO.Contains(FProductionNO));
            }
            if (!string.IsNullOrEmpty(BeginFCurDate))
            {
                if (string.IsNullOrEmpty(EndFCurDate))
                {
                    EndFCurDate = "9999-12-31";
                }
                extWhere = "(FCurDate>=@BeginFCurDate and FCurDate<=@EndFCurDate)";
                whereobj.Add("BeginFCurDate", BeginFCurDate);
                whereobj.Add("EndFCurDate", EndFCurDate);
            }
            if (!string.IsNullOrEmpty(FVoucherNo))
            {
                expression = expression.And(t => t.FVoucherNo.Contains(FVoucherNo));
            }
            if(string.IsNullOrEmpty(type))
            {
                type = "CC";
            }
            expression = expression.And(p => p.FDocuTypeNO == type);
            return service.FindList(expression, pagination, extWhere, whereobj);
        }
    }
}
